clear
clear matrix
set mem 500m
set more off
set matsize 800
set varabbrev off

*data directory
global dta "C:\Users\ajsaenz\Dropbox (JPAL LAC)\Preu\Preu\data"

use "${dta}/base_preu_final.dta"

**************************************
* Some new variables and housekeeping*
**************************************

egen real_group=group(t_grupo_lev5)
g t_cohorte_grupo=t_cohorte*1000000+real_group
drop real_group
replace t_completed=0 if t_completed==.

*Interactions
gen bl_school_quality_high=0 if bl_school_quality_2~=.
replace bl_school_quality_high=1 if bl_school_quality_2==2

forvalues y=0/1 {
forvalues x=0/1 {
gen int_ols_c`y'_s`x'=0 if bl_school_quality_high~=.
replace int_ols_c`y'_s`x'=t_itt if t_cohorte==`y' & bl_school_quality_high==`x'
gen int_iv_c`y'_s`x'=0  if bl_school_quality_high~=.
replace int_iv_c`y'_s`x'=t_completed if t_cohorte==`y' & bl_school_quality_high==`x'
gen int_top_c`y'_s`x'=0  if bl_school_quality_high~=. & el_prep_sum_toppreu~=.
replace int_top_c`y'_s`x'=el_prep_sum_toppreu if t_cohorte==`y' & bl_school_quality_high==`x'
}
gen int_ols_c`y'=0
replace int_ols_c`y'=t_itt if t_cohorte==`y'
gen int_iv_c`y'=0
replace int_iv_c`y'=t_completed if t_cohorte==`y'
gen int_top_c`y'=0 if el_prep_sum_toppreu~=.
replace int_top_c`y'=el_prep_sum_toppreu if t_cohorte==`y'
gen int_ols_s`y'=0 if bl_school_quality_high~=.
replace int_ols_s`y'=t_itt if bl_school_quality_high==`y'
gen int_iv_s`y'=0 if bl_school_quality_high~=.
replace int_iv_s`y'=t_completed if bl_school_quality_high==`y'
gen int_top_s`y'=0 if bl_school_quality_high~=. & el_prep_sum_toppreu~=.
replace int_top_s`y'=el_prep_sum_toppreu if bl_school_quality_high==`y'
}
gen int_c1_s1=0 if bl_school_quality_high~=.
replace int_c1_s1=1 if t_cohorte==1 & bl_school_quality_high==1

*PERCENT CORRECT ANSWERS IN MATH
g psucorrect_percent=(ela_psucorrect_math/70) if t_cohorte==0
replace psucorrect_percent=(ela_psucorrect_math/75) if t_cohorte==1 
replace psucorrect_percent=psucorrect_percent*70

*Tracking experiment dummies
gen bla_psuscore_science=bla_psuscore_math
foreach x in leng math science {
gen t_peer_`x'_group_high=0 if t_peer_`x'_group~=.
replace t_peer_`x'_group_high=1 if t_peer_`x'_group==1
gen t_peer_`x'_group_mixed=0 if t_peer_`x'_group~=.
replace t_peer_`x'_group_mixed=1 if t_peer_`x'_group==3
gen t_peer_`x'_group_track=1 if t_peer_`x'_group~=.
replace t_peer_`x'_group_track=0 if t_peer_`x'_group==3
gen b_`x'=bla_psuscore_`x'
gen b_`x'_2=b_`x'*b_`x'
gen b_`x'_3=b_`x'*b_`x'*b_`x'
}

**************************************
*FIRST TABLES AND FIGURES (Tables 1-5)
**************************************

*Annual Expenditures on PSU Exam Preparation for People in the Control Group Attending a Top Preuniversitario (Table 2) [exchange rate: $CH/$US=500/1]
capture: gen el_prep_preu_cost_year_us=el_prep_preu_cost_year/500
capture: g pay500=0 if el_prep_preu_cost_year~=.
capture: g pay300=0 if el_prep_preu_cost_year~=.
capture: g pay000=0 if el_prep_preu_cost_year~=.
replace pay000=1 if el_prep_preu_cost_year~=. & el_prep_preu_cost_year==0
replace pay300=1 if el_prep_preu_cost_year~=. & el_prep_preu_cost_year<300000
replace pay500=1 if el_prep_preu_cost_year~=. & el_prep_preu_cost_year<500000
	*C1, LQ
sum el_prep_preu_cost_year_us if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==0 & bl_school_quality_high==0 , d
sum pay000 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==0 & bl_school_quality_high==0 
sum pay300 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==0 & bl_school_quality_high==0 
sum pay500 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==0 & bl_school_quality_high==0 
	*C1, HQ
sum el_prep_preu_cost_year_us if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==0 & bl_school_quality_high==1 , d
sum pay000 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==0 & bl_school_quality_high==1
sum pay300 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==0 & bl_school_quality_high==1
sum pay500 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==0 & bl_school_quality_high==1
	*C2, LQ
sum el_prep_preu_cost_year_us if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==1 & bl_school_quality_high==0 , d
sum pay000 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==1 & bl_school_quality_high==0
sum pay300 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==1 & bl_school_quality_high==0
sum pay500 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==1 & bl_school_quality_high==0
	*C3, HQ
sum el_prep_preu_cost_year_us if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==1 & bl_school_quality_high==1 , d
sum pay000 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==1 & bl_school_quality_high==1
sum pay300 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==1 & bl_school_quality_high==1
sum pay500 if t_itt==0 & el_prep_sum_toppreu==1 & t_cohorte==1 & bl_school_quality_high==1

*Sample Composition of the Scholarship Experiment (Table3)
	*Applicants
count
bysort t_cohorte: count
	*Offered
count if t_itt==1
bysort t_cohorte: count if t_itt==1
	*Accepted
count if t_tot==1
bysort t_cohorte: count if t_tot==1
	*Rejected
count if t_reject==1
bysort t_cohorte: count if t_reject==1
	*Not offered
count if t_itt==0
bysort t_cohorte: count if t_itt==0

*Sample Composition of the Tracking Experiment (Table 4)
	*Language
count if t_peer_leng_group==1
bysort t_cohorte: count if t_peer_leng_group==1
count if t_peer_leng_group==2
bysort t_cohorte: count if t_peer_leng_group==2
count if t_peer_leng_group==3
bysort t_cohorte: count if t_peer_leng_group==3
	*Math
count if t_peer_math_group==1
bysort t_cohorte: count if t_peer_math_group==1
count if t_peer_math_group==2
bysort t_cohorte: count if t_peer_math_group==2
count if t_peer_math_group==3
bysort t_cohorte: count if t_peer_math_group==3
	*Science
count if t_peer_science_group==1
bysort t_cohorte: count if t_peer_science_group==1
count if t_peer_science_group==2
bysort t_cohorte: count if t_peer_science_group==2
count if t_peer_science_group==3
bysort t_cohorte: count if t_peer_science_group==3

*Table 5 is at the end of this do-file

******************************
*BALANCE (Tables 6-10)
******************************
global baseline bl_genero bla_school_dep bla_school_simce_leng bla_school_simce_math bla_grade11_att bla_nota bla_psuscore_leng ///
bla_psuscore_math bl_educacion_exp_psuscore_leng bl_educacion_exp_psuscore_math bl_educacion_preu bl_educacion_preppsu_preu bl_perception_control_index /// 
bl_identificacion_3 bl_familia_educacion_mama bl_otros_casa_index bl_educacion_cantlibros

*Balance between groups in the scholarship experiment (Table 6)
	*Panel A
foreach x of varlist $baseline {
sum `x' if t_cohorte==0
xi3: reg `x' t_cohorte 
}
	*Panel B
foreach x of varlist $baseline {
sum `x' if t_itt==0
xi3: reg `x' t_itt 
}

*Balance between groups in the scholarship experiment (Table 7)
gen t_tot2=0
replace t_tot2=1 if t_tot==0
	*Panel A
foreach x of varlist $baseline {
sum `x' if t_tot==1 & t_itt==1
xi3: reg `x' t_tot2 if t_itt==1 
}
	*Panel B
foreach x of varlist $baseline {
sum `x' if t_tot==1 & t_itt==1 & t_cohorte==0
xi3: reg `x' t_tot2 if t_itt==1 & t_cohorte==0
}
	*Panel C
foreach x of varlist $baseline {
sum `x' if t_tot==1 & t_itt==1 & t_cohorte==1
xi3: reg `x' t_tot2 if t_itt==1 & t_cohorte==1
}

*Balance between Attriters across Survey and Treatment Status (Table 9)
	*Panel A
foreach x of varlist $baseline {
sum `x' if t_surveyed==0 
xi3: reg `x' t_surveyed  
}
	*Panel B
foreach x of varlist $baseline {
sum `x' if t_surveyed==0 & t_itt==0 
xi3: reg `x' t_itt if t_surveyed==0
}

*Table 10 is at the end of this do-file

**********************************
*IMPACT ESTIMATIONS (Tables 11-21)
**********************************

*Generating X's
global X  "bla_psuscore_leng bla_psuscore_math bl_familia_educacion_lugar bl_familia_educacion_mama bl_ingreso bl_familia_apoyo bl_educacion_tiempoestudio bla_nota_dev bla_school_simce_average bl_school_quality_high bl_educacion_cantlibros bl_identificacion_3 bl_perception_control_index bl_genero t_cohorte i.t_cohorte_grupo"
global XP "bla_psuscore_leng bla_psuscore_math bl_familia_educacion_lugar bl_familia_educacion_mama bl_ingreso bl_familia_apoyo bl_educacion_tiempoestudio bla_nota_dev bla_school_simce_average bl_school_quality_high bl_educacion_cantlibros bl_identificacion_3 bl_perception_control_index bl_genero t_cohorte"


*Impact on Access to Preu (Table 11)
	*Accepted Scholarship for Preu UC
xi: reg t_tot               t_itt 
xi: reg t_tot               t_itt int_ols_c1 t_cohorte
xi: reg t_tot               t_itt int_ols_s1 bl_school_quality_high
xi: reg t_tot               t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 t_cohorte bl_school_quality_high int_c1_s1
	*Completed Preu UC
xi: reg t_completed         t_itt 
xi: reg t_completed         t_itt int_ols_c1 t_cohorte
xi: reg t_completed         t_itt int_ols_s1 bl_school_quality_high bl_school_quality_high
xi: reg t_completed         t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 t_cohorte bl_school_quality_high  int_c1_s1
	*Attended a Top Preuniversitario
xi: reg el_prep_sum_toppreu t_itt 
xi: reg el_prep_sum_toppreu t_itt int_ols_c1 t_cohorte
xi: reg el_prep_sum_toppreu t_itt int_ols_s1 bl_school_quality_high bl_school_quality_high
xi: reg el_prep_sum_toppreu t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 t_cohorte bl_school_quality_high  int_c1_s1


*12th GRADE PERFORMANCE (Table 12) 
foreach x of varlist ela_nem ela_grade12_att ela_grade12_repeat {
xi: reg `x'   t_itt $X
xi: reg `x'   t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 $X
xi: ivreg `x' (el_prep_sum_toppreu=t_itt) $X
xi: ivreg `x' (el_prep_sum_toppreu int_top_c1 int_top_s1 int_top_c1_s1 = t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1) $X
}

*Table 13 is at the end of this do

*IMPACT ON PSU PARTICIPATION (Table 14)
foreach x of varlist ela_nopsu_all ela_nopsu_area ela_nopsu_history  {
xi: reg   `x' t_itt $X
xi: reg   `x' t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 $X
xi: ivreg `x' (el_prep_sum_toppreu=t_itt) $X
xi: ivreg `x' (el_prep_sum_toppreu int_top_c1 int_top_s1 int_top_c1_s1 = t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1) $X
}


*PSU RESULTS (TABLE 15)
	*Math and Spanish
foreach x of varlist ela_psuscore_leng ela_psuscore_math {
xi: reg `x'   t_itt $X
xi: reg `x'   t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 $X
xi: ivreg `x' (el_prep_sum_toppreu=t_itt) $X
xi: ivreg `x' (el_prep_sum_toppreu int_top_c1 int_top_s1 int_top_c1_s1 = t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1) $X
}
	*History and Science, correcting selection bias using a Heckit procedure
gen take_history=1-ela_nopsu_history
gen take_science=1-ela_nopsu_science

xi: dprobit take_history t_itt bl_educacion_exp_psuscore_leng  $X
predict probva_history, xb
gen l_history=normalden(probva_history)/normal( probva_history)
xi: dprobit take_history t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 bl_educacion_exp_psuscore_leng  $X
predict probva_history_int, xb
gen l_history_int=normalden(probva_history_int)/normal( probva_history_int)
xi: dprobit take_science t_itt bl_educacion_exp_psuscore_math $X
predict probva_science, xb
gen l_science=normalden(probva_science)/normal( probva_science)
xi3: dprobit take_science t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 bl_educacion_exp_psuscore_math $X
predict probva_science_int, xb
gen l_science_int=normalden(probva_science_int)/normal( probva_science_int)

foreach x in science history {
xi: reg ela_psuscore_`x'   t_itt l_`x' $X
xi: reg ela_psuscore_`x'   t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 l_`x'_int $X
xi: ivreg ela_psuscore_`x' (el_prep_sum_toppreu=t_itt) l_`x' $X
xi: ivreg ela_psuscore_`x' (el_prep_sum_toppreu int_top_c1 int_top_s1 int_top_c1_s1 = t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1) l_`x'_int $X
}

*HIGHER EDUCATION OUTCOMES (Table 16)
foreach x of varlist ela_enrolled_he_firstyear ela_enrolled_u_firstyear ela_enrolled_ip_firstyear ela_enrolled_cft_firstyear {
xi: reg `x'   t_itt $X
xi: reg `x'   t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 $X
xi: ivreg `x' (el_prep_sum_toppreu= t_itt) $X
xi: ivreg `x' (el_prep_sum_toppreu int_top_c1 int_top_s1 int_top_c1_s1 = t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1) $X
}

*LABOR MARKET OUTCOMES, INSTITUTION QUALITY
***Some selection bias correction for entering HE
xi: dprobit ela_enrolled_he_firstyear t_itt $X
predict probva_enrolled, xb
gen l_enrolled=normalden(probva_enrolled)/normal( probva_enrolled)
xi3: dprobit ela_enrolled_he_firstyear t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 $X
predict probva_enrolled_int, xb
gen l_enrolled_int=normalden(probva_enrolled_int)/normal( probva_enrolled_int)


g lwage=ln((ela_fl_max_wage+ela_fl_min_wage)/2)
g lwage_e=ln(ela_fl_emp_rate*(ela_fl_max_wage+ela_fl_min_wage)/2)
g wage=((ela_fl_max_wage+ela_fl_min_wage)/2)
g wage_e=(ela_fl_emp_rate*(ela_fl_max_wage+ela_fl_min_wage)/2)

*Some details
replace ela_fl_averagepsu=. if ela_fl_averagepsu==0
replace ela_fl_emp_rate=. if ela_fl_emp_rate==0
replace wage=. if wage==0
replace wage_e=. if wage_e==0


*MORE ON LABOR MARKET OUTCOMES: PREDICTED (Table 17)

g pred_psu_sci=ela_psuscore_science
replace pred_psu_sci=0 if ela_nopsu_science==1
g pred_psu_his=ela_psuscore_history
replace pred_psu_his=0 if ela_nopsu_history==1
	*Table A.3: Weights for the average PSU score and wage indexes
xi: reg lwage_e ela_nem ela_psuscore_leng ela_psuscore_math ela_nopsu_history ela_nopsu_science pred_psu_his pred_psu_sci $X if t_itt==0
predict pred_w, xb
xi: reg ela_fl_averagepsu ela_nem ela_psuscore_leng ela_psuscore_math ela_nopsu_history ela_nopsu_science  pred_psu_his pred_psu_sci $X if t_itt==0
predict pred_psu, xb

foreach x of varlist pred_w pred_psu {
xi: reg `x'   t_itt $X
xi: reg `x' t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 $X
xi: ivreg `x' (el_prep_sum_toppreu= t_itt) $X
xi: ivreg `x' (el_prep_sum_toppreu int_top_c1 int_top_s1 int_top_c1_s1 = t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1) $X
}

*View on Mineduc Expenditure (Table 18)
foreach x of varlist el_educexpenditure_psu el_educexpenditure_exphe el_educexpenditure_supervision el_educexpenditure_freehe el_educexpenditure_qualityhe {
xi: reg `x'   t_itt $X
xi: reg `x'   t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1  $X
xi: ivreg `x' (el_prep_sum_toppreu= t_itt) $X
xi: ivreg `x' (el_prep_sum_toppreu int_top_c1 int_top_s1 int_top_c1_s1 = t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1) $X
}

*Table 19 and Figure 5 are constructed with the do-file named Monotonicity

*PEER EFFECTS EXPERIMENT

*Standardized variables
sum ela_psuscore_leng if t_itt==0
*Variable	Obs	Mean	Std. Dev.	Min	Max
*ela_psus~eng	774	594.4625	82.04005	312	808
sum ela_psuscore_math if t_itt==0
*Variable	Obs	Mean	Std. Dev.	Min	Max
*ela_psusco~h	774	601.0969	88.84507	264	850
sum ela_psuscore_science if t_itt==0
*Variable	Obs	Mean	Std. Dev.	Min	Max
*ela_psusc~ce	605	586.8017	88.0599	272	850
gen ela_sd_leng=(ela_psuscore_leng-594.4625)/82.04005
gen ela_sd_math=(ela_psuscore_math-601.0969)/88.84507
gen ela_sd_science=(ela_psuscore_science-586.8017)/88.0599

*Differences between groups
gen bla_psuscore_science=bla_psuscore_math
foreach x in leng math science {
gen t_peer_`x'_group_high=0 if t_peer_`x'_group~=.
replace t_peer_`x'_group_high=1 if t_peer_`x'_group==1
gen t_peer_`x'_group_mixed=0 if t_peer_`x'_group~=.
replace t_peer_`x'_group_mixed=1 if t_peer_`x'_group==3
gen b_`x'=bla_psuscore_`x'
gen b_`x'_2=b_`x'*b_`x'
gen b_`x'_3=b_`x'*b_`x'*b_`x'
}

*Generating Interactions
foreach x in leng math science {
gen i_t_peer_`x'_group_high_c1=t_peer_`x'_group_high*t_cohorte
gen i_t_peer_`x'_group_mixed_c1=t_peer_`x'_group_mixed*t_cohorte
gen i_t_peer_`x'_tracking_c1=t_peer_`x'_tracking*t_cohorte
}


*Differences between Classes
foreach x in leng math science {
foreach z in ela_psuscore_`x' {
xi: reg `z' t_peer_`x'_group_high t_peer_`x'_group_mixed b_`x'_2 b_`x'_3 $XP i.preu_block_`x'
xi: reg `z' t_peer_`x'_group_high i_t_peer_`x'_group_high_c1 t_peer_`x'_group_mixed i_t_peer_`x'_group_mixed_c1 b_`x'_2 b_`x'_3 $XP i.preu_block_`x'
}
}


*Impact of Tracking (Table 20)
foreach x in leng math science {
foreach z in ela_psuscore_`x'  {
xi: reg `z' t_peer_`x'_tracking b_`x'_2 b_`x'_3 $XP i.preu_block_`x'
xi: reg `z' t_peer_`x'_tracking b_`x'_2 b_`x'_3 i_t_peer_`x'_tracking_c1 $XP i.preu_block_`x'
}
}

*REGRESSION DISCONTINUITY (Table 21 and Figure 4)
bysort preu_block_leng: egen med_group_leng=median(bla_psuscore_leng)
g corrected_leng=bla_psuscore_leng-med_group_leng  if t_peer_leng_group~=3 & t_peer_leng_group~=.
bysort preu_block_math: egen med_group_math=median(bla_psuscore_math)
g corrected_math=bla_psuscore_math-med_group_math  if t_peer_math_group~=3 & t_peer_math_group~=.
bysort preu_block_science: egen med_group_science=median(bla_psuscore_math)
g corrected_science=bla_psuscore_math-med_group_science  if t_peer_science_group~=3 & t_peer_science_group~=.

rd ela_psuscore_leng corrected_leng
rd ela_psuscore_math corrected_math 
rd ela_psuscore_science corrected_science
foreach x in 0 1 {
rd ela_psuscore_leng corrected_leng if t_cohorte==`x'
rd ela_psuscore_math corrected_math if t_cohorte==`x'
rd ela_psuscore_science corrected_science if t_cohorte==`x'
}

rd ela_psuscore_leng corrected_leng, gr
rd ela_psuscore_math corrected_math, gr
rd ela_psuscore_science corrected_science, gr



label var preu_class_leng_id "identifies the class attended in leng"
label var num_mates_class_leng "number of classmates in leng class"
label var bl_peers_mean_psuscore_leng "classmates baseline score leng"
label var bl_peers_var_psuscore_leng "var of classmates baseline psu score leng"
label var bl_peers_sd_psuscore_leng "var of classmates baseline psu score leng"


*Differences between groups (Table 22)

xi: ivreg2 ela_psuscore_math ( bl_peers_mean_psuscore_math bl_peers_sd_psuscore_math=i.t_peer_math_group) bla_psuscore_math b_math_2 b_math_3  i.preu_block_math, first
xi: ivreg2 ela_psuscore_science ( bl_peers_mean_psuscore_sc bl_peers_sd_psuscore_sc=i.t_peer_science_group) bla_psuscore_science b_science_2 b_science_3  i.preu_block_science, first
xi: ivreg2 ela_psuscore_leng ( bl_peers_mean_psuscore_leng bl_peers_sd_psuscore_leng=i.t_peer_leng_group) bla_psuscore_leng b_leng_2 b_leng_3  i.preu_block_leng, first


*APPENDIX A.1
foreach x of varlist bl_educacion_exp_psuscore_math bl_familia_educacion_mama bla_nota_dev bla_school_simce_average bl_educacion_cantlibros bl_perception_control_index {
egen ddd=mean(`x')
g t_int=t_itt*(`x'-ddd)
xi: reg ela_psuscore_leng t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 t_int $X
lincom t_int
local tstat=r(estimate)/r(se)
local pval = tprob(r(df), abs(`tstat'))
drop ddd
drop t_int
}

*APPENDIX A.2
	*Math and Spanish
foreach x of varlist ela_psucorrect_leng ela_psucorrect_math   {
xi: reg `x'   t_itt $X
xi: reg `x'   t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 $X
xi: ivreg `x' (el_prep_sum_toppreu=t_itt) $X
xi: ivreg `x' (el_prep_sum_toppreu int_top_c1 int_top_s1 int_top_c1_s1 = t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1) $X
}
	*History and Science, using Heckit procedure
foreach x in science history {
xi: reg ela_psucorrect_`x'   t_itt l_`x' $X
xi: reg ela_psucorrect_`x'   t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1 l_`x'_int $X
xi: ivreg ela_psucorrect_`x' (el_prep_sum_toppreu=t_itt) l_`x' $X
xi: ivreg ela_psucorrect_`x' (el_prep_sum_toppreu int_top_c1 int_top_s1 int_top_c1_s1 = t_itt int_ols_c1 int_ols_s1 int_ols_c1_s1) l_`x'_int $X
}

*Table A.3: Weights for the average PSU score and wage indexes (This table comes from constructing predicted outcomes for Table 17)

***************************
*Reminding Tables and Figures

*FIGURE 2
use "${dta}\psu_year", clear

gen ins_total=inscritos_total/matricula
label var ins_total "Enrolled in PSU / Total enrollment of 12th graders"
gen ins_year=inscritos_year/matricula
label var ins_year "Enrolled in PSU, only of the promotion of that year / Total enrollment of 12th graders"
gen rend_total=rendidos_total/matricula
label var rend_total "Rendered PSU / Total enrollment of 12th graders"
gen rend_year=rendidos_year/matricula
label var rend_year "Rendered PSU, only of the promotion of that year / Total enrollment of 12th graders"

drop if year==2013

set scheme s1mono

twoway (line ins_total year) (line rend_total year,								///
		lpattern(dash)															///
		xline(2011, lpattern(dot)) 												///
		xtitle("") 																///
		ytitle(Ratio (Total PSU / Total 12th graders))							///
		legend(on order(1 2) label (1 "PSU Enrollment") label(2 "PSU Participation") region(lwidth(none))))

twoway (line ins_year year) (line rend_year year,								///
		lpattern(dash)															///
		xline(2011, lpattern(dot)) 												///
		xtitle("") 																///
		ytitle(Ratio (Total PSU / Total 12th graders))							///					
		legend(on order(1 2) label (1 "PSU Enrollment") label(2 "PSU Participation") region(lwidth(none))))
	
*Sample vulnerability (Table 5)
	*Column 1 and 2 (Chile)
use "${dta}\matricula_2010.dta", clear
merge n:n rbd using dta/estab_2m_2010
* Keep if cod_ense Educacion Media
keep if cod_ense3==4 | cod_ense3==6  
* Drop if Particular Pagado
drop if cod_depe2==3 
* Keep if 4to medio
keep if cod_grado==4
tab grupo if cod_ense3==4
tab grupo if cod_ense3==4 & cod_reg_rbd==13

	*Column 3 (Experiment)
use "${dta}\base_preu_final.dta", clear
gen rbd=bla_school_rbd
merge n:n rbd using dta/estab_2m_2010.dta, keepusing(grupo)
drop if _merge==2
drop _merge
tab grupo

*TABLE 10
use "${dta}\base_monitoreo_collapsed.dta", clear
gen trackexp=0
replace trackexp=1 if tracking!=.
lab var trackexp "=1 if in tracking experiment (high, low or mixed class)"

global classroom a9 m_timetostartclass m_controloverclass m_punctualteacher m_latestudents m_teacherunderstandscontents m_teacherintegrates m_teacheradapts ///
m_teachertalksmistakes m_studentsattitutudecontents m_teacherasks m_studentsanswer m_studentsask m_teacheranswers m_irespectfulteacher m_irespectfulstudent ///
m_relationship m_powerpoint m_teachersendshomework m_givesheets m_referencesheets

*Differences in Classroom Outcomes by Cohort and Experimental Assignment (Table 10)
foreach z of varlist $classroom  {
*Panel A
xi: reg `z' jpal t_cohorte i.class_subject 
*Panel B
xi: reg `z' t_cohorte i.class_subject if jpal==1
*Panel C
xi: reg `z' trackexp t_cohorte i.class_subject if jpal==1
*Panel D
xi: reg `z' tracking t_cohorte i.class_subject if jpal==1
*Panel E
xi: reg `z' m_alto t_cohorte i.class_subject if tracking==1 & jpal==1
}

*TABLE 13
use "${dta}\psu20102011.dta", clear

*Correlation between School's SIMCE and PSU Results for each Cohort (Table 13)
replace psu_lenguaje=. if psu_lenguaje<150
replace psu_matematica=. if psu_matematica<150
gen psu=(psu_lenguaje+psu_matematica)/2 if psu_lenguaje!=. & psu_matematica!=.
replace psu=. if psu==0

g out=0 if dependencia~="."
replace out=1 if dependencia=="Particular Pagado"
g cohorte=ano-2010
g simce_d=simce-250

xi3: areg  psu  simce*cohorte if out==0 &  (simce>=197 & simce<=339), r a(rbd)
xi3: areg  psu  simce*cohorte if out==1 &  (simce>=197 & simce<=339), r a(rbd)

